package com.xiaojf.xlog.appender.es.configure;

import com.xiaojf.xlog.appender.AccessLogAppender;
import com.xiaojf.xlog.appender.es.AccessLogEsAppender;
import com.xiaojf.xlog.appender.es.repository.AccessLogRepository;
import com.xiaojf.xlog.aspect.AccessLogAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import javax.annotation.Resource;

/**
 * rdb 保存访问日志配置
 *
 * @author Ade.Xiao 2021/3/12 13:42
 */
@ConditionalOnMissingBean(AccessLogAppender.class)
@ConditionalOnProperty(prefix = "xlog.access", name = "appender-type", havingValue = "es")
@EnableElasticsearchRepositories(basePackageClasses = {AccessLogRepository.class})
@Configuration
public class AccessLogEsAppenderConfigure {
    @Resource
    private AccessLogRepository accessLogRepository;

    /**
     * 控制台方式记录访问日志 ， 默认方式
     *
     * @return {@link AccessLogAspect}
     * @author Ade.Xiao 2021-03-11 14:36:19
     */
    @Bean
    @Primary
    public AccessLogAppender accessLogAppender() {
        return new AccessLogEsAppender(accessLogRepository);
    }
}
